package org.webdb.webapp.jackson;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.SerializerProvider;
import org.postgis.MultiPolygon;

public class MultiPolygonSerializer {

    private PolygonJSONSerializer polygonSerializer;

    public MultiPolygonSerializer(PolygonJSONSerializer polygonSerializer) {
        this.polygonSerializer = polygonSerializer;
    }

    public void serialize(MultiPolygon multiPolygon, JsonGenerator jgen,
            SerializerProvider provider) throws JsonGenerationException,
            IOException {
        jgen.writeStartObject();
        jgen.writeStringField(GEOJSON.TYPE_ATTRIBUTE,
                GEOJSON.MULTIPOLYGON_TYPE_NAME);
        jgen.writeFieldName(GEOJSON.COORDINATES_ATTRIBUTE);
        jgen.writeStartArray();
        for (int i = 0; i < multiPolygon.numPolygons(); i++) {
            polygonSerializer.writePolygon(multiPolygon.getPolygon(i), jgen);
        }
        jgen.writeEndArray();
        jgen.writeEndObject();
    }

}
